import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:crypto/crypto.dart';

class BaseTools {
  static bool isEmpty(Object? object) {
    if (null == object) {
      return true;
    }
    if (object is String && object.isEmpty) {
      return true;
    } else if (object is List && object.isEmpty) {
      return true;
    } else if (object is Map && object.isEmpty) {
      return true;
    }
    return false;
  }

  ///minFrom，maxFrom:UI显示最小值和最大值    minTo,maxTo:实际作用最小值和最大值    orgValue：UI当前值
  ///return 当前实际作用值
  static num countValueFromRangeMin(
      num minFrom, num maxFrom, num minTo, num maxTo, num orgValue) {
    if (maxFrom - minFrom == 0) {
      return minTo;
    }
    num percent = (orgValue - minFrom) / (maxFrom - minFrom);
    num rValue = (maxTo - minTo) * percent + minTo;
    if (rValue > maxTo) {
      rValue = maxTo;
    }
    if (rValue < minTo) {
      rValue = minTo;
    }
    return rValue;
  }

  ///int a = 1;  =>  String a = '01';
  static String timeFormat(int value) {
    var string = value.toString();
    return string.length != 1 ? string : '0' + string;
  }

  /// 24小时 -> 12小时
  static String timeFormat12(int value) {
    if (value > 12 && value < 24) {
      value = value - 12;
    } else if (value == 0) {
      value = 12;
    }

    var string = value.toString();
    return string.length != 1 ? string : '0' + string;
  }

  static String minutesFormat(
      int totalMinutes, String hourUnit, String minutesUnit) {
    String hour = (totalMinutes / 60).floor().toString();
    hour = hour == '0' ? '' : '$hour $hourUnit';
    String minutes = (totalMinutes % 60).toString();
    minutes = minutes == '0' ? '' : '$minutes $minutesUnit';
    return '$hour $minutes';
  }

  // static String generateMD5(String data) {
  //   var content = new Utf8Encoder().convert(data);
  //   var digest = md5.convert(content);
  //   // 这里其实就是 digest.toString()
  //   return hex.encode(digest.bytes);
  // }

  static String timestamp2String(int total, String dayUnit) {
    int daySe = 86400; //60*60*24 一天的秒
    int hourSe = 3600; //60*60 一小时的秒
    int minuteSe = 60;

    int remainSeconds = 0;

    String dayStr = '';
    String hourStr = '';
    String minuteStr = '';
    String secondStr = '';

    if (total >= daySe) {
      dayStr = (total / daySe).floor().toString() + ' $dayUnit ';
      remainSeconds = total % daySe;
    } else {
      remainSeconds = total;
    }

    if (remainSeconds >= hourSe) {
      hourStr = timeFormat((remainSeconds / hourSe).floor()) + ':';
      remainSeconds = remainSeconds % hourSe;
    } else {
      hourStr = '00:';
    }

    if (remainSeconds >= minuteSe) {
      minuteStr = timeFormat((remainSeconds / minuteSe).floor()) + ':';
      remainSeconds = remainSeconds % minuteSe;
    } else {
      minuteStr = '00:';
    }

    if (remainSeconds > 0) {
      secondStr += timeFormat(remainSeconds);
    } else {
      secondStr = '00';
    }

    return '$dayStr$hourStr$minuteStr$secondStr' + ' ';
  }

  ///单词转换为单个字母
  static String breakWord(String word) {
    if (word.isEmpty) {
      return word;
    }
    String breakWord = '';
    word.runes.forEach((element) {
      breakWord += String.fromCharCode(element);
      breakWord += '\u200B';
    });
    return breakWord;
  }

  ///枚举类型转string
  static String enumToString(o) => o.toString().split('.').last;

  ///string转枚举类型
  static T enumFromString<T>(Iterable<T> values, String value) {
    return values.firstWhereOrNull(
            (type) => type.toString().split('.').last == value) ??
        values.first;
  }

  ///预设时间判断
  static DateTime getPresetTime({bool isFixedTime = false, int addMinute = 2}) {
    DateTime t = DateTime.now();
    int minute = 0;
    bool isHour = false;

    DateTime y;

    if (isFixedTime) {
      if (t.minute > 0 && t.minute < 5) {
        minute = 5;
      } else if (t.minute >= 5 && t.minute < 10) {
        minute = 10;
      } else if (t.minute >= 10 && t.minute < 15) {
        minute = 15;
      } else if (t.minute >= 15 && t.minute < 20) {
        minute = 20;
      } else if (t.minute >= 20 && t.minute < 25) {
        minute = 25;
      } else if (t.minute >= 25 && t.minute < 30) {
        minute = 30;
      } else if (t.minute >= 30 && t.minute < 35) {
        minute = 35;
      } else if (t.minute >= 35 && t.minute < 40) {
        minute = 40;
      } else if (t.minute >= 40 && t.minute < 45) {
        minute = 45;
      } else if (t.minute >= 45 && t.minute < 50) {
        minute = 50;
      } else if (t.minute >= 50 && t.minute < 55) {
        minute = 55;
      } else if (t.minute >= 55 && t.minute < 59) {
        minute = 0;
        isHour = true;
      }
      DateTime s = DateTime(t.year, t.month, t.day, t.hour, minute);
      y = s.add(Duration(hours: isHour ? 1 : 0));

      return y;
    } else {
      y = t.add(Duration(minutes: addMinute));
      return y;
    }
  }

  static int getIntValue(num value) {
    late int _value;
    if (value is int) {
      _value = value;
    } else {
      _value = value.toInt();
    }

    return _value;
  }
}
